package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;


import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtil;
import com.gopay.bis.dao.payconfig.BisBankExtConfigDAO;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.yspay.ebank.YspayMessage;
import com.gopay.common.domain.bis.BaseBisBankExtConfig;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;
/**
 * 银盛对象封装
 * @YsPayManager.java
 * @author lilili
 * @2016-3-24 上午11:44:47  www.gopay.com.cn Inc.All rights reserved.
 */
public class YsPayManager implements BankManagerRemoteService {

	@Resource(name = "ysPayConfig")
	private Map<String, String> ysPayConfig;
	
	@Resource(name = "ysPayBankServiceClientHna")
	private BankService ysPayBankServiceClientHna;

	@Resource(name = "ysPayBankServiceClient")
	private BankService ysPayBankServiceClient;
	
	@Resource(name="bisBankExtConfigDAO")
	private BisBankExtConfigDAO bisBankExtConfigDAO;
	
	@Override
	public Object sign(BaseMessage msg) throws GopayException {
		// 获取银行接口通道配置信息
//		BaseBisBankExtConfig bankConfig = bisBankExtConfigDAO.findExtConfigByBankCodeAndBankBusProd(msg.getBankBusProd(), msg.getBank());
		msg.setBankOrderId("YS" + TransUtil.processOrderIdTo14Digit(msg.getOrderId()));
		//设置银联接口银行码
//		msg.setSpecial2(bankConfig.getIntrfaceBankCode());
		YspayMessage message = (YspayMessage)msg;
	    message.setVer(ysPayConfig.get("YSPAY.VERSION"));
	    message.setSrc(message.getMerID());
	    message.setMsgCode(ysPayConfig.get("YSPAY.PAY.MSGCODE"));
	    message.setCur(ysPayConfig.get("YSPAY.CUR"));
	    message.setBusiCode(ysPayConfig.get("YSPAY.BUSICODE"));
	    message.setShopDate(TransUtil.getCurrentDateStr());
	    message.setNote(ysPayConfig.get("YSPAY.PAY.NOTE"));
	    message.setTimeout(ysPayConfig.get("YSPAY.TIMEOUT"));
	    message.setPgUrl(message.getPayNotifyUrl());
	    message.setBgUrl(message.getPayRuturnUrl());
	    if(msg.getBank().equals(BankConstants.BANK_LOGO_CMBC)){
	        message.setBankType(ysPayConfig.get("YSPAY.CMBC.BANKTYPE"));
	    }else if(msg.getBank().equals(BankConstants.BANK_LOGO_CCB)){
	        message.setBankType(ysPayConfig.get("YSPAY.CCB.BANKTYPE"));
	    }else if(msg.getBank().equals(BankConstants.BANK_LOGO_ABC)){
            message.setBankType(ysPayConfig.get("YSPAY.ABC.BANKTYPE"));
        }else if(msg.getBank().equals(BankConstants.BANK_LOGO_BOC)){
            message.setBankType(ysPayConfig.get("YSPAY.BOC.BANKTYPE"));
        }else if(msg.getBank().equals(BankConstants.BANK_LOGO_ICBC)){
            message.setBankType(ysPayConfig.get("YSPAY.ICBC.BANKTYPE"));
        }else if(msg.getBank().equals(BankConstants.BANK_LOGO_CMB)){
            message.setBankType(ysPayConfig.get("YSPAY.CMB.BANKTYPE"));
        }else if(msg.getBank().equals(BankConstants.BANK_LOGO_BOCOM)){
            message.setBankType(ysPayConfig.get("YSPAY.BOCOM.BANKTYPE"));
        }
	    message.setBankAccountType(ysPayConfig.get("YSPAY.BANKACCOUNTTYPE"));
	    if (!message.isSupportCredit()) {
	        message.setBankAccountType(ysPayConfig.get("YSPAY.INCHARGE.BANKACCOUNTTYPE"));
	    }
	    message.setOrderAmount(msg.getAmount().movePointRight(2).toString());
	    message.setUserCode(message.getSrc());
	    message.setName(ysPayConfig.get("YSPAY.NAME"));
	    message.setOutStlmCd("YSPAY");
	    BankLogUtils.printB2CManagerSignSendLog(message);
	    if(message.isHna()){
            message = (YspayMessage)ysPayBankServiceClientHna.sign(message);
        }else{
            message = (YspayMessage)ysPayBankServiceClient.sign(message);  
        }
        BankLogUtils.printB2CManagerSignReturnLog(message);
        return message;
	}

	@Override
	public Object verify(BaseMessage msg) throws GopayException {
	    YspayMessage message = (YspayMessage)msg;
        String plainMessage = message.getMsg();
        message.setPlainMessage(plainMessage);
        BankLogUtils.printB2CManagerVerifySendLog(message);
        if(message.isHna()){
            message = (YspayMessage) ysPayBankServiceClientHna.verify(message);
        }else{
            message = (YspayMessage) ysPayBankServiceClient.verify(message);  
        }
        BankLogUtils.printB2CManagerVerifyReturnLog(message, message.isVerify());
        return message;
	}

	
	@Override
	public Object query(Object obj) throws GopayException {
	    YspayMessage message = (YspayMessage)obj;
        message.setVer(ysPayConfig.get("YSPAY.VERSION"));
        message.setSrc(ysPayConfig.get("YSPAY.SRC"));
        message.setMsgCode(ysPayConfig.get("YSPAY.QUERY.MSGCODE"));
        message.setTradeSn("");
        BankLogUtils.printB2CManagerQuerySendLog(message, message);
        if(message.isHna()){
            message = (YspayMessage)ysPayBankServiceClientHna.query(message);
        }else{
            message = (YspayMessage)ysPayBankServiceClient.query(message);  
        }
        BankLogUtils.printB2CManagerQueryReturnLog(message);
        return message;
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
